<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        font-size: 28px;
      }
    </style>
  </head>

  <body>
    <textarea id="textareaScript" cols="30" rows="20"></textarea>

    <div>
      <button type="button" id="btnCreateScript">创建脚本</button>
    </div>

    <script>
      textareaScript.value = `;(function(){
            console.log("location.href:", location.href)
        })()`;

      btnCreateScript.onclick = function () {
        const scriptEL = document.createElement("script");
        // 使用脚本内容初始化blob最终创建出一个url地址
        const scriptSrc = URL.createObjectURL(new Blob([textareaScript.value]));
        scriptEL.src = scriptSrc;
        document.body.appendChild(scriptEL);
      };
    </script>
  </body>
</html>
